package com.example.tse.rbyair.PKhome.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.tse.rbhttpservice.service.goods.model.GoodsGetVirtualCatListList;
import com.example.tse.rbyair.R;
import com.example.tse.rbyair.util.RbImageLoader;

import java.util.List;

/**
 * Created by hp on 16/1/25.
 */
public class MainSortItemFragment extends Fragment {
    private ImageView iv1,iv2,iv3,iv4;
    private View view ;
    private List<GoodsGetVirtualCatListList> lists;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.mainitemfragment, null);
        iv1 = (ImageView)view.findViewById(R.id.iv1);
        iv2 = (ImageView)view.findViewById(R.id.iv2);
        iv3 = (ImageView)view.findViewById(R.id.iv3);
        iv4 = (ImageView)view.findViewById(R.id.iv4);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        lists = (List<GoodsGetVirtualCatListList>) getArguments().getSerializable("lists");

        if(lists.size() == 4){
            RbImageLoader.displayImage(lists.get(0).getImg(), iv1, RbImageLoader.getProductDetialOptions());
            RbImageLoader.displayImage(lists.get(1).getImg(), iv2, RbImageLoader.getProductDetialOptions());
            RbImageLoader.displayImage(lists.get(2).getImg(), iv3, RbImageLoader.getProductDetialOptions());
            RbImageLoader.displayImage(lists.get(3).getImg(), iv4, RbImageLoader.getProductDetialOptions());
            iv1.setOnClickListener(new MyClickListener(0));
            iv2.setOnClickListener(new MyClickListener(1));
            iv3.setOnClickListener(new MyClickListener(2));
            iv4.setOnClickListener(new MyClickListener(3));
        }else if(lists.size() == 3){
            RbImageLoader.displayImage(lists.get(0).getImg(), iv1, RbImageLoader.getProductDetialOptions());
            RbImageLoader.displayImage(lists.get(1).getImg(), iv2, RbImageLoader.getProductDetialOptions());
            RbImageLoader.displayImage(lists.get(2).getImg(), iv3, RbImageLoader.getProductDetialOptions());
            iv4.setVisibility(View.GONE);
            iv1.setOnClickListener(new MyClickListener(0));
            iv2.setOnClickListener(new MyClickListener(1));
            iv3.setOnClickListener(new MyClickListener(2));
        }else if(lists.size() == 2){
            RbImageLoader.displayImage(lists.get(0).getImg(), iv1, RbImageLoader.getProductDetialOptions());
            RbImageLoader.displayImage(lists.get(1).getImg(), iv2, RbImageLoader.getProductDetialOptions());
            iv3.setVisibility(View.GONE);
            iv4.setVisibility(View.GONE);
            iv1.setOnClickListener(new MyClickListener(0));
            iv2.setOnClickListener(new MyClickListener(1));
        }else if(lists.size() == 1){
            RbImageLoader.displayImage(lists.get(0).getImg(), iv1, RbImageLoader.getProductDetialOptions());
            iv2.setVisibility(View.GONE);
            iv3.setVisibility(View.GONE);
            iv4.setVisibility(View.GONE);
            iv1.setOnClickListener(new MyClickListener(0));
        }
    }

    class MyClickListener implements View.OnClickListener {
        int position = 0;
        public MyClickListener(int position){
            this.position = position;
        }
        @Override
        public void onClick(View v) {
//            Intent intent = new Intent(getActivity(), ReclassifyActivity.class);
//            intent.putExtra("name", lists.get(position).getVirtualCatName());
//            //TODO 统计
//            intent.putExtra("rudder_route", IntegrateConst.NEW_SORT_ROUTE+"|"+IntegrateConst.NEW_SORT_POSITION_ONE + (position+1));
//            intent.putExtra("rudder_position", IntegrateConst.NEW_SORT_POSITION_ONE + (position+1));
//            intent.putExtra("virtualCatId", lists.get(position).getVirtualCatId());
//            startActivity(intent);
        }

    }
}
